<?php

class DccController extends Zend_Controller_Action
{
    function preDispatch()
    {
		if(!$_SESSION['identity'])
        {
            $this->_redirect("/login");
        }
		
         //Make menu
         $mod_menu = new Db_Tables2_BDictMenu();
         $this->view->main_menu = $mod_menu->MenuDecolation();
    }
	
    function indexAction()
	{
		$this->_redirect("/dcc/companies");
	}
	
    function companiesAction()
    {
		$params = $this->_request->getParams();
		
		$this->view->title = "店铺列表";
		
		$params_pool = array();
		$mod_tables = new Db_Joins_TablesJoins();
		
		if($params['keyword'])
    	{
    		$mod_tables->keyword = $params['keyword'];
    		$this->view->keyword = $params['keyword'];
    		$params_pool[] = "keyword=".$params['keyword'];
    	}
		
		if($params['staff_name'])
    	{
    		$mod_tables->staff_name = $params['staff_name'];
    		$this->view->staff_name = $params['staff_name'];
    		$params_pool[] = "staff_name=".$params['staff_name'];
    	}
		
		if(!$params['page_id'])
    	{
    		$page_id = 1;
    	}else{
    		$page_id = $params['page_id'];
    	}
    	
    	if($page_id)
    	{
    		$mod_tables->page_id = $page_id;
    		$this->view->page_id = $page_id;
    	}
		
		$this->view->data = $result = $mod_tables->GetDccCompanies();
		
		//make pagination
    	if(!empty($params_pool))
    	{
    		$params_string = implode("&", $params_pool);
    	}else{
    		$params_string = "";
    	}
    	
    	if(1 == $page_id)
    	{
    		$first_page_string = "";
    		$previous_page_string = "";
    	}else{
    		$first_page_string = "<a href='?page_id=1&".$params_string."'>最前页</a>&nbsp;&nbsp;&nbsp;";
    		$previous_page_string = "<a href='?page_id=".($page_id-1)."&".$params_string."'>上一页</a>&nbsp;&nbsp;&nbsp;";
    	}
    	
    	if($page_id < $result['total_pages'] )
    	{
    		$next_page_string = "<a href='?page_id=".($page_id+1)."&".$params_string."'>下一页</a>&nbsp;&nbsp;&nbsp;";
    		$last_page_string = "<a href='?page_id=".$result['total_pages']."&".$params_string."'>最后页</a>&nbsp;&nbsp;&nbsp;";
    	}else{
    		$next_page_string = "";
    		$last_page_string = "";
    	}
    	
		$this->view->params_string = $params_string;
    	$this->view->pagination = $first_page_string.$previous_page_string.$next_page_string.$last_page_string;
		
		if($params['msg'])
		{
				switch ($params['msg'])
				{
					case 1:
						$this->view->msg = "操作成功";
						break;
					case 2:
						$this->view->msg = "写入商铺失败";
						break;
					case 3:
						$this->view->msg = "关键参数缺失";
						break;
					case 4:
						$this->view->msg = "找不到该商铺";
						break;
					case 5:
						$this->view->msg = "操作失败";
						break;
				}
		}
	}
	
	function editUserAction()
	{
		$params = $this->_request->getParams();
		
		if($params['user_id'])
		{
			$this->view->user_id = $params['user_id'];
			
			$maozhua_wb = Zend_Registry::get('maozhua_wb');
			$select = $maozhua_wb->select();
			$select->from("users", "*");
			$select->where("user_id = ?", $params['user_id']);
			
			$row = $maozhua_wb->fetchRow($select);
			
			if($row)
			{
				$this->view->title = "编辑".$row['user_name'];
				$this->view->data = $row;
				$mod_t_area = new Db_Tables2_TArea();
				$this->view->init_cities = $mod_t_area->GetCities($row['province_id']);
				
				$mod_params = new Db_Tables2_BDictParams();
				$sales_role_id = array();
				$sales_role_id[] = $mod_params->GetVal("LEVEL-1");
				$sales_role_id[] = $mod_params->GetVal("LEVEL-2");

				$mod_user = new Db_Tables2_User();
				$this->view->sales = $mod_user->GetUsersByRole($sales_role_id);
				
				$mod_accounts = new Db_Tables2_Accounts();
				$this->view->accounts = $mod_accounts->GetAccounts($params['user_id']);
			}else{
				$this->_redirect("/maozhua/users/msg/2");
			}
		}else{
			$this->_redirect("/maozhua/users/msg/2");
		}
		
		$mod_t_area = new Db_Tables2_TArea();
		$this->view->provinces = $mod_t_area->GetProvinces();
		
		if($params['msg'])
		{
			switch ($params['msg'])
			{
				case 1:
					$this->view->msg = "用户信息修改成功。<a href='/maozhua/users'>【返回用户列表】</a>";
					break;
			}
		}
	}
	
	function editUserSubmitAction()
	{
		$params = $this->_request->getParams();
		
//		$mod_ext = new Sys_Extensions();
//		$mod_ext->FormatArray($params);
//		die;
		
		if($params['user_id'])
		{
			$data = array(
				"user_name" => trim($params['user_name']),
				"cellphone" => trim($params['cellphone']),
				"expired_date" => trim($params['expired_date']),
				"emp_id" => trim($params['emp_id']),
				"status" => trim($params['status']),
				"province_id" => trim($params['provinceId']),
				"city_id" => trim($params['cityId'])
			);
			
			if(trim($params['pwd']))
			{
				$data['pwd_sha1'] = sha1(trim($params['pwd']));
			}
			
			$maozhua_wb = Zend_Registry::get('maozhua_wb');
			$where = "user_id = ".$params['user_id'];
			$maozhua_wb->update("users", $data, $where);
			
			//初始化汽车之家
			$select = $maozhua_wb->select();
			$select->from("accounts", "*");
			$select->where("user_id = ?", $params['user_id']);
			$select->where("account_type = ?", 1);
			
			$row = $maozhua_wb->fetchRow($select);
			
			if($row)
			{
				$data = array(
					"user_name" => trim($params['type1_user_name']),
					"dealer_id" => trim($params['type1_dealer_id']),
					"cookies" => NULL,
					"err_login_times" => 0
				);
				
				if(trim($params['type1_pwd']))
				{
					$data['pwd'] = trim($params['type1_pwd']);
				}
				
				if(trim($params['type1_dealer_id']) && trim($params['type1_user_name']) && (trim($params['type1_pwd']) || $row['pwd']) && $params['type1_status'] && $params['status'])
				{
					$data['status'] = 1;
				}else{
					$data['status'] = 0;
					$data['server_id']=NULL;
					$data['cookies']=NULL;
				}
				
				$where = "account_id = ".$row['account_id'];
				$maozhua_wb->update("accounts", $data, $where);
			}else{
				$data = array(
					"user_id" => $params['user_id'],
					"account_type" => 1,
					"user_name" => trim($params['type1_user_name']),
					"ctime" => date("Y-m-d H:i:s"),
					"dealer_id" => trim($params['type1_dealer_id']),
					"province_id" => trim($params['provinceId']),
					"city_id" => trim($params['cityId'])
				);
				
				if(trim($params['type1_pwd']))
				{
					$data['pwd'] = trim($params['type1_pwd']);
				}
				
				if(trim($params['type1_dealer_id']) && trim($params['type1_user_name']) && trim($params['type1_pwd']) && $params['type1_status'] && $params['status'])
				{
					$data['status'] = 1;
				}else{
					$data['status'] = 0;
					$data['server_id']=NULL;
					$data['cookies']=NULL;
				}

				$maozhua_wb->insert("accounts", $data);
			}
			
			//初始化汽车之家APP
			$select = $maozhua_wb->select();
			$select->from("accounts", "*");
			$select->where("user_id = ?", $params['user_id']);
			$select->where("account_type = ?", 4);
			
			$row = $maozhua_wb->fetchRow($select);
			
			if($row)
			{
				$data = array(
					"user_name" => trim($params['type4_user_name']),
					"dealer_id" => trim($params['type4_dealer_id'])
				);
				
				if(trim($params['type4_pwd']))
				{
					$data['pwd'] = trim($params['type4_pwd']);
				}
				
				if(trim($params['type4_dealer_id']) && trim($params['type4_user_name']) && (trim($params['type4_pwd']) || $row['pwd']) && $params['type4_status'] && $params['status'])
				{
					$data['status'] = 1;
				}else{
					$data['status'] = 0;
					$data['server_id']=NULL;
					$data['cookies']=NULL;
				}
				
				$where = "account_id = ".$row['account_id'];
				$maozhua_wb->update("accounts", $data, $where);
			}else{
				$data = array(
					"user_id" => $params['user_id'],
					"account_type" => 4,
					"user_name" => trim($params['type4_user_name']),
					"ctime" => date("Y-m-d H:i:s"),
					"dealer_id" => trim($params['type4_dealer_id']),
					"province_id" => trim($params['provinceId']),
					"city_id" => trim($params['cityId'])
				);
				
				if(trim($params['type4_pwd']))
				{
					$data['pwd'] = trim($params['type4_pwd']);
				}
				
				if(trim($params['type4_dealer_id']) && trim($params['type4_user_name']) && trim($params['type4_pwd']) && $params['type4_status'] && $params['status'])
				{
					$data['status'] = 1;
				}else{
					$data['status'] = 0;
					$data['server_id']=NULL;
					$data['cookies']=NULL;
				}

				$maozhua_wb->insert("accounts", $data);
			}
			
			//初始化易车
			$select = $maozhua_wb->select();
			$select->from("accounts", "*");
			$select->where("user_id = ?", $params['user_id']);
			$select->where("account_type = ?", 2);
			
			$row = $maozhua_wb->fetchRow($select);
			
			if($row)
			{
				$data = array(
					"user_name" => trim($params['type2_user_name']),
					"dealer_id" => trim($params['type2_dealer_id']),
					"cookies" => NULL,
					"err_login_times" => 0
				);
				
				if(trim($params['type2_pwd']))
				{
					$data['pwd'] = trim($params['type2_pwd']);
				}
				
				if(trim($params['type2_dealer_id']) && trim($params['type2_user_name']) && (trim($params['type2_pwd']) || $row['pwd']) && $params['type2_status'] && $params['status'])
				{
					$data['status'] = 1;
				}else{
					$data['status'] = 0;
					$data['server_id']=NULL;
					$data['cookies']=NULL;
				}
				
				$where = "account_id = ".$row['account_id'];
				$maozhua_wb->update("accounts", $data, $where);
			}else{
				if($params['cityId'])
				{
					$dealers_db = Zend_Registry::get("dealers_db");
					$dselect = $dealers_db->select();
					$dselect->from("t_area", array("yicheProvinceId", "yicheCityId"));
					$dselect->where("cityId = ?", $params['cityId']);
					$drow = $dealers_db->fetchRow($dselect);
				}else{
					$drow = NULL;
				}
				
				$data = array(
					"user_id" => $params['user_id'],
					"account_type" => 2,
					"user_name" => trim($params['type2_user_name']),
					"ctime" => date("Y-m-d H:i:s"),
					"dealer_id" => trim($params['type2_dealer_id'])
				);
				
				if($drow)
				{
					$data['province_id'] = $drow['yicheProvinceId'];
					$data['city_id'] = $drow['yicheCityId'];
				}
				
				if(trim($params['type2_pwd']))
				{
					$data['pwd'] = trim($params['type2_pwd']);
				}
				
				if(trim($params['type2_dealer_id']) && trim($params['type2_user_name']) && trim($params['type2_pwd']) && $params['type2_status'] && $params['status'])
				{
					$data['status'] = 1;
				}else{
					$data['status'] = 0;
					$data['server_id']=NULL;
					$data['cookies']=NULL;
				}

				$maozhua_wb->insert("accounts", $data);
			}
			
			$this->_redirect("/maozhua/users/msg/1");
		}else{
			$this->_redirect("/maozhua/users/msg/2");
		}
	}
	
    function callHistoryAction()
    {
		$params = $this->_request->getParams();
		
		$this->view->title = "通话记录";
		
		$params_pool = array();
		$mod_reports = new Db_Joins_ReportsJoins();
    	
		if($params['date_start'])
		{
			$date_start = $params['date_start'];
		}else{
			$date_start = NULL;
		}
    	
		if($params['date_end'])
		{
			$date_end = $params['date_end'];
		}else{
			$date_end = NULL;
		}
		
		if($date_start)
    	{
    		$mod_reports->date_start = $date_start;
    		$this->view->date_start = $date_start;
    		$params_pool[] = "date_start=".$date_start;
    	}
		
		if($date_end)
    	{
    		$mod_reports->date_end = $date_end;
    		$this->view->date_end = $date_end;
    		$params_pool[] = "date_end=".$date_end;
    	}
		
		if($params['call_in_num'])
    	{
    		$mod_reports->call_in_num = $params['call_in_num'];
    		$this->view->call_in_num = $params['call_in_num'];
    		$params_pool[] = "call_in_num=".$params['call_in_num'];
    	}
		
		if($params['call_phone'])
    	{
    		$mod_reports->call_phone = $params['call_phone'];
    		$this->view->call_phone = $params['call_phone'];
    		$params_pool[] = "call_phone=".$params['call_phone'];
    	}
		
		if($params['call_in_out'])
    	{
    		$mod_reports->call_in_out = $params['call_in_out'];
    		$this->view->call_in_out = $params['call_in_out'];
    		$params_pool[] = "call_in_out=".$params['call_in_out'];
    	}
		
		if(is_numeric($params['is_pick']))
    	{
    		$mod_reports->is_pick = $params['is_pick'];
    		$this->view->is_pick = $params['is_pick'];
    		$params_pool[] = "is_pick=".$params['is_pick'];
    	}
		
		if($params['user_name'])
    	{
    		$mod_reports->user_name = $params['user_name'];
    		$this->view->user_name = $params['user_name'];
    		$params_pool[] = "user_name=".$params['user_name'];
    	}
		
		if(!$params['page_id'])
    	{
    		$page_id = 1;
    	}else{
    		$page_id = $params['page_id'];
    	}
    	
    	if($page_id)
    	{
    		$mod_reports->page_id = $page_id;
    		$this->view->page_id = $page_id;
    	}

    	if($params['company_id']){
		    $mod_reports->company_id = $params['company_id'];
		    $this->view->company_id = $params['company_id'];
        }

		if($no_auth)
		{
			$this->view->data = $result = array(
				'ct' => 0,
				'total_pages' => 0,
				'current_page_info' => array()
			);
		}else{
			$this->view->data = $result = $mod_reports->CallHistory();

		}
		
//		$ext = new Sys_Extensions();
//		$ext->FormatArray($this->view->data);die;
		//make pagination
    	if(!empty($params_pool))
    	{
    		$params_string = implode("&", $params_pool);
    	}else{
    		$params_string = "";
    	}
    	
    	if(1 == $page_id)
    	{
    		$first_page_string = "";
    		$previous_page_string = "";
    	}else{
    		$first_page_string = "<a href='?page_id=1&".$params_string."'>最前页</a>&nbsp;&nbsp;&nbsp;";
    		$previous_page_string = "<a href='?page_id=".($page_id-1)."&".$params_string."'>上一页</a>&nbsp;&nbsp;&nbsp;";
    	}
    	
    	if($page_id < $result['total_pages'] )
    	{
    		$next_page_string = "<a href='?page_id=".($page_id+1)."&".$params_string."'>下一页</a>&nbsp;&nbsp;&nbsp;";
    		$last_page_string = "<a href='?page_id=".$result['total_pages']."&".$params_string."'>最后页</a>&nbsp;&nbsp;&nbsp;";
    	}else{
    		$next_page_string = "";
    		$last_page_string = "";
    	}
    	
		$this->view->params_string = $params_string;
    	$this->view->pagination = $first_page_string.$previous_page_string.$next_page_string.$last_page_string;
	}
	
	function companiesAddAction()
	{
		$params = $this->_request->getParams();
		$this->view->title = "创建店铺";
		
		$mod_t_fac = new Db_Tables2_TFactory();
		$this->view->factories = $mod_t_fac->GetFactories();
		
		if($params['error'])
		{
				switch ($params['error'])
				{
					case 1:
						$this->view->msg = "关键参数缺失";
						break;
					case 2:
						$this->view->msg = "写入数据失败";
						break;
					default:
						$this->view->msg = "其他错误";
						break;
				}
		}
	}
	
	function companiesAddSubmitAction()
	{
		$params = $this->_request->getParams();
		
		if($params['company_name'] && $params['cellphone'] && $params['pwd'])
		{
			$data = array(
				"company_name" => trim($params['company_name']),
				"expired_date" => trim($params['expired_date'])?trim($params['expired_date']):NULL,
				"start_date" => trim($params['start_date']),
				"status" => $params['status'],
				"client_display_number" => trim($params['client_display_number'])?trim($params['client_display_number']):NULL,
				"balance1" => 0,
				"balance2" => 0,
				"staff_name" => trim($params['staff_name'])?trim($params['staff_name']):NULL,
				"total_users" => 0,
				"history_call_out_mins" => 0,
				"history_call_in_mins" => 0,
				"history_fee" => 0,
				"dealer_brand" => trim($params['dealer_brand']),
				"dealer_address" => trim($params['dealer_address']),

			);
			
			$maozhua_wb = Zend_Registry::get('maozhua_wb');
			
			if($maozhua_wb->insert("dcc_companies", $data))
			{
				$select = $maozhua_wb->select();
				$select->from("dcc_companies", "company_id");
				$select->order("company_id DESC");
				$row = $maozhua_wb->fetchRow($select);
				
				if($row)
				{
					$company_id = $row['company_id'];
				}
			}
			
			if($company_id)
			{
				$mod_t_fac = new Db_Tables2_TFactory();
				$factories = $mod_t_fac->GetFactories();

				if($params['factory_id_1'])
				{
					$data = array(
						"company_id" => $company_id,
						"factory_id" => $params['factory_id_1'],
						"factory_name" => $factories[$params['factory_id_1']]
					);
					
					$maozhua_wb->insert("dcc_companies_factory", $data);
				}
				
				if($params['factory_id_2'])
				{
					$data = array(
						"company_id" => $company_id,
						"factory_id" => $params['factory_id_2'],
						"factory_name" => $factories[$params['factory_id_2']]
					);
					
					$maozhua_wb->insert("dcc_companies_factory", $data);
				}
				
				if($params['factory_id_3'])
				{
					$data = array(
						"company_id" => $company_id,
						"factory_id" => $params['factory_id_3'],
						"factory_name" => $factories[$params['factory_id_3']]
					);
					
					$maozhua_wb->insert("dcc_companies_factory", $data);
				}
				
				if($params['biz_start'] && $params['biz_end'])
				{
					$data = array(
						"company_id" => $company_id,
						"biz_start" => $params['biz_start'],
						"biz_end" => $params['biz_end']
					);
					
					$maozhua_wb->insert("dcc_companies_biz_hour", $data);
				}
				
				if($params['period'])
				{
					$data = array(
						"company_id" => $company_id,
						"type_id" => 1,
						"period" => $params['period']
					);
					
					$maozhua_wb->insert("dcc_companies_period", $data);
				}
				
				if($params['cellphone'] && $params['pwd'])
				{
					$data = array(
						"company_id" => $company_id,
						"user_name" => trim($params['user_name']),
						"user_type" => 1,
						"cellphone" => trim($params['cellphone']),
						"pwd_sha1" => sha1($params['pwd']),
						"status" => 1
					);
					
					$maozhua_wb->insert("dcc_users", $data);
				}
				
				$mod_level = new Db_Tables2_DccCompaniesLevel();
				$mod_level->CompanyInit($company_id);
				
				$this->_redirect("/dcc/companies/msg/1");
			}else{
				$this->_redirect("/dcc/companies/msg/2"); //写入商铺失败
			}
		}else{
			$this->_redirect("/dcc/companies/msg/3"); //关键参数缺失
		}
	}
	
	function companiesEditAction()
	{
		$params = $this->_request->getParams();
		
		if($params['company_id'])
		{
			$this->view->company_id = $params['company_id'];
			
			$maozhua_wb = Zend_Registry::get('maozhua_wb');
			$select = $maozhua_wb->select();
			$select->from("dcc_companies as c", array("company_name", "expired_date", "start_date", "status", "client_display_number", "staff_name","czht_dealer_id","dealer_brand","dealer_address"));
			$select->joinLeft("dcc_users as u", "u.company_id=c.company_id", array("user_id", "user_name", "cellphone"));
			$select->where("c.company_id = ?", $params['company_id']);
			$select->where("u.user_type = ?", 1);
			
			$row = $maozhua_wb->fetchRow($select);
			
			if($row)
			{
				$this->view->title = "编辑".$row['company_name'];
				$this->view->data = $row;
				
				$mod_t_fac = new Db_Tables2_TFactory();
				$this->view->factories = $mod_t_fac->GetFactories();
				
				$maozhua_wb = Zend_Registry::get('maozhua_wb');
				$select = $maozhua_wb->select();
				$select->from("dcc_companies_biz_hour", array("biz_start", "biz_end"));
				$select->where("company_id = ?", $params['company_id']);

				$row = $maozhua_wb->fetchRow($select);
				
				if($row)
				{
					$this->view->biz_time = $row;
				}else{
					$this->view->biz_time = array();
				}
				
				$maozhua_wb = Zend_Registry::get('maozhua_wb');
				$select = $maozhua_wb->select();
				$select->from("dcc_companies_period", array("period"));
				$select->where("company_id = ?", $params['company_id']);
				$select->where("type_id = ?", 1);

				$row = $maozhua_wb->fetchRow($select);
				
				if($row)
				{
					$this->view->period = $row;
				}else{
					$this->view->period = array();
				}
				
				$maozhua_wb = Zend_Registry::get('maozhua_wb');
				$select = $maozhua_wb->select();
				$select->from("dcc_companies_factory", array("factory_id"));
				$select->where("company_id = ?", $params['company_id']);

				$rows = $maozhua_wb->fetchAll($select);
				
				$this->view->fac = array();
				
				if($rows)
				{
					foreach($rows as $row)
					{
						$this->view->fac[] = $row['factory_id'];
					}
					
				}
			}else{
				$this->_redirect("/dcc/companies/msg/4");
			}
		}else{
			$this->_redirect("/dcc/companies/msg/3");
		}
		
		$mod_t_area = new Db_Tables2_TArea();
		$this->view->provinces = $mod_t_area->GetProvinces();
		
		if($params['msg'])
		{
			switch ($params['msg'])
			{
				case 1:
					$this->view->msg = "签约公司信息修改成功。<a href='/clients/signers'>【返回签约公司列表】</a>";
					break;
			}
		}
	}
	
	function companiesEditSubmitAction()
	{
		$params = $this->_request->getParams();
		
		if($params['company_name'] && $params['cellphone'] && $params['company_id'])
		{
			$set = array(
				"company_name" => trim($params['company_name']),
				"expired_date" => trim($params['expired_date'])?trim($params['expired_date']):NULL,
				"start_date" => trim($params['start_date']),
				"status" => $params['status'],
				"client_display_number" => trim($params['client_display_number'])?trim($params['client_display_number']):NULL,
				"staff_name" => trim($params['staff_name'])?trim($params['staff_name']):NULL,
                "czht_dealer_id"=>trim($params['czht_dealer_id']),
                "dealer_brand"=>trim($params['dealer_brand']),
                "dealer_address"=>trim($params['dealer_address']),
			);
			
			$maozhua_wb = Zend_Registry::get('maozhua_wb');
			
			$where = "company_id=".$params['company_id'];
			$maozhua_wb->update("dcc_companies", $set, $where);

			$set = array(
				"user_name" => trim($params['user_name'])?trim($params['user_name']):NULL,
				"cellphone" => trim($params['cellphone'])?trim($params['cellphone']):NULL
			);
			
			if($params['pwd'])
			{
				$set['pwd_sha1'] = sha1($params['pwd']);
			}
			
			$where = "company_id=".$params['company_id']." and user_type=1";
			$maozhua_wb->update("dcc_users", $set, $where);
			
			$where = "company_id=".$params['company_id'];
			$maozhua_wb->delete("dcc_companies_factory", $where);

            if(is_numeric($params['status']) && !$params['status'])
            {
                $del = array(
                    "user_name" => NULL,
                    "cellphone" => NULL,
                    "pwd_sha1" => NULL,
                    "call_phone" => NULL,
                    "call_id" => NULL,
                );
                $where = "company_id=".$params['company_id']." AND user_type=1";
                $maozhua_wb->update("dcc_users",$del,$where);
            }

			$mod_t_fac = new Db_Tables2_TFactory();
			$factories = $mod_t_fac->GetFactories();

			if($params['factory_id_1'])
			{
				$data = array(
					"company_id" => $params['company_id'],
					"factory_id" => $params['factory_id_1'],
					"factory_name" => $factories[$params['factory_id_1']]
				);

				$maozhua_wb->insert("dcc_companies_factory", $data);
			}

			if($params['factory_id_2'])
			{
				$data = array(
					"company_id" => $params['company_id'],
					"factory_id" => $params['factory_id_2'],
					"factory_name" => $factories[$params['factory_id_2']]
				);

				$maozhua_wb->insert("dcc_companies_factory", $data);
			}

			if($params['factory_id_3'])
			{
				$data = array(
					"company_id" => $params['company_id'],
					"factory_id" => $params['factory_id_3'],
					"factory_name" => $factories[$params['factory_id_3']]
				);

				$maozhua_wb->insert("dcc_companies_factory", $data);
			}
			
			$set = array(
				"biz_start" => trim($params['biz_start'])?trim($params['biz_start']):NULL,
				"biz_end" => trim($params['biz_end'])?trim($params['biz_end']):NULL
			);
			
			$where = "company_id=".$params['company_id'];
			$maozhua_wb->update("dcc_companies_biz_hour", $set, $where);
			
			if($params['period'])
			{
				$set = array(
					"period" => trim($params['period'])?trim($params['period']):NULL
				);

				$where = "company_id=".$params['company_id'];
				$maozhua_wb->update("dcc_companies_period", $set, $where);
			}
			
			$this->_redirect("/dcc/companies/msg/1");
		}else{
			$this->_redirect("/dcc/companies/msg/3"); //关键参数缺失
		}
	}
	
	function companiesBalanceAction()
	{
		$params = $this->_request->getParams();
		$this->view->title = "充值";
		
		if($params['company_id'])
		{
			$this->view->company_id = $params['company_id'];
		}else{
			$this->_redirect("/dcc/companies/msg/3"); //关键参数缺失
		}
	}
	
	function companiesBalanceSubmitAction()
	{
		$params = $this->_request->getParams();
		
		if($params['company_id'] && $params['action_value'])
		{
			$mod_logs_balance = new Db_Tables2_DccLogsBalance();
			$mod_logs_balance->company_id = $params['company_id'];
			$mod_logs_balance->staff_id = $_SESSION['identity']['userid'];
			$mod_logs_balance->staff_name = $_SESSION['identity']['realname'];
			$mod_logs_balance->balance_type = $params['balance_type'];
			$mod_logs_balance->action_type = $params['action_type'];
			$mod_logs_balance->action_value = trim($params['action_value']);
			$mod_logs_balance->tips = "商务系统操作";
			
			if($mod_logs_balance->AddRecord())
			{
				$this->_redirect("/dcc/companies/msg/1");
			}else{
				$this->_redirect("/dcc/companies/msg/5"); //关键参数缺失
			}
		}else{
			$this->_redirect("/dcc/companies/msg/3"); //关键参数缺失
		}
	}
	
    function companiesUserAction()
    {
		$params = $this->_request->getParams();
		
		if($params['company_id'])
		{
			$this->view->company_id = $params['company_id'];
			
			$maozhua_wb = Zend_Registry::get('maozhua_wb');
			$select = $maozhua_wb->select();
			$select->from("dcc_companies", array("company_name"));
			$select->where("company_id = ?", $params['company_id']);
			
			$row = $maozhua_wb->fetchRow($select);
			
			if($row)
			{
				$this->view->title = "坐席管理 - ".$row['company_name'];
				
				$select = $maozhua_wb->select();
				$select->from("dcc_users", "*");
				$select->where("company_id = ?", $params['company_id']);
				$select->where("user_type = ?", 2);

				$this->view->users = $maozhua_wb->fetchAll($select);
			}else{
				$this->_redirect("/dcc/companies/msg/4");
			}
		}else{
			$this->_redirect("/dcc/companies/msg/3"); //关键参数缺失
		}
		
		if($params['msg'])
		{
				switch ($params['msg'])
				{
					case 1:
						$this->view->msg = "操作成功";
						break;
					case 2:
						$this->view->msg = "天润创建用户失败";
						break;
					case 3:
						$this->view->msg = "关键参数缺失";
						break;
					case 4:
						$this->view->msg = "找不到该商铺";
						break;
					case 5:
						$this->view->msg = "操作失败";
						break;
				}
		}
	}
	
	function companiesUserAddAction()
	{
		$params = $this->_request->getParams();
		
		if($params['company_id'])
		{
			$this->view->company_id = $params['company_id'];
			
			$maozhua_wb = Zend_Registry::get('maozhua_wb');
			$select = $maozhua_wb->select();
			$select->from("dcc_companies", array("company_name"));
			$select->where("company_id = ?", $params['company_id']);
			
			$row = $maozhua_wb->fetchRow($select);
			
			if($row)
			{
				$this->view->title = "创建坐席 - ".$row['company_name'];
			}else{
				$this->_redirect("/dcc/companies-user/company_id/".$params['company_id']."/msg/4");
			}
		}else{
			$this->_redirect("/dcc/companies/msg/3"); //关键参数缺失
		}
	}
	
	function companiesUserAddSubmitAction()
	{
		$params = $this->_request->getParams();
		
		if($params['company_id'] && $params['create_num'])
		{
			$mod_tianrun = new Sys_Tianrun();
			$fb = $mod_tianrun->CreateUser($params['create_num']);
			
			if(!empty($fb))
			{
				$maozhua_wb = Zend_Registry::get('maozhua_wb');
				
				foreach($fb as $cno)
				{
					$data = array(
						"company_id" => $params['company_id'],
						"user_type" => 2,
						"status" => 1,
						"call_id" => $cno
					);
					$maozhua_wb->insert("dcc_users", $data);
				}

				$set = array(
				    "user_id"=>$_SESSION['identity']['userid'],
				    "company_id"=>$params['company_id'],
				    "content"=>"成功创建了".$params['create_num']."个坐席",
                    "times"=>date("Y-m-d H:i:s")
                );
				$maozhua_wb->insert("dcc_change_log",$set);

				$mod_call_status = new Db_Tables2_DccUsersCallStatus();
				$mod_call_status->InitCompanyUsers($params['company_id']);
				
				$this->_redirect("/dcc/companies-user/company_id/".$params['company_id']."/msg/1");
			}else{
                $this->_redirect("/dcc/companies-user/company_id/".$params['company_id']."/msg/2");
			}
		}else{
			$this->_redirect("/dcc/companies/msg/3"); //关键参数缺失
		}
	}
	
	function companiesUserEditAction()
	{
		$params = $this->_request->getParams();
		
		if($params['user_id'])
		{
			$this->view->user_id = $params['user_id'];
			
			$maozhua_wb = Zend_Registry::get('maozhua_wb');
			$select = $maozhua_wb->select();
			$select->from("dcc_users", "*");
			$select->where("user_id = ?", $params['user_id']);
			
			$row = $maozhua_wb->fetchRow($select);
			
			if($row)
			{
				$this->view->title = "编辑坐席 - ".$row['call_id'];
				$this->view->data = $row;
			}else{
				$this->_redirect("/dcc/companies/msg/3");
			}
		}else{
			$this->_redirect("/dcc/companies/msg/3"); //关键参数缺失
		}
	}

    function companiesUserEditSubmitAction()
    {
        $params = $this->_request->getParams();

        if($params['user_id'])
        {
            $maozhua_wb = Zend_Registry::get('maozhua_wb');
            $select = $maozhua_wb->select();
            $select->from("dcc_users", "*");
            $select->where("user_id = ?", $params['user_id']);

            $urow = $maozhua_wb->fetchRow($select);

            $set = array(
                "user_name" => trim($params['user_name']),
                "cellphone" => trim($params['cellphone']),
                "status" => $params['status'],
            );

            if($params['pwd'])
            {
                $set['pwd_sha1'] = sha1($params['pwd']);
            }

            if(trim($params['call_phone']))
            {
                $mod_tianrun = new Sys_Tianrun();
                $fb = $mod_tianrun->ClientOnline($urow['call_id'], 1, $params['call_phone'], 1);

                if(isset($fb['result']) && isset($fb['msg']))
                {
                    if('success' == $fb['result'] && '操作成功' == $fb['msg'])
                    {
                        $mod_tianrun->ClientOffline($urow['call_id']);
                    }

                    $set['call_phone'] = trim($params['call_phone']);
                }
            }

            if(is_numeric($params['status']) && !$params['status'])
            {
                $set['call_phone'] = NULL;
            }
            $where = "user_id = ".$urow['user_id'];

            $maozhua_wb->update("dcc_users", $set, $where);

            $set = array(
                "user_id"=>$_SESSION['identity']['userid'],
                "company_id"=>$urow['company_id'],
                "content"=>"编辑了坐席：user_name=".$params['user_name'].",cellphone=".$params['cellphone'].",call_phone=".$params['call_phone'].",status=".$params['status'],
                "times"=>date("Y-m-d H:i:s")
            );
            $maozhua_wb->insert("dcc_change_log",$set);

            $this->_redirect("/dcc/companies-user/company_id/".$urow['company_id']."/msg/1");
        }else{
            $this->_redirect("/dcc/companies/msg/3"); //关键参数缺失
        }
    }
}